package com.gjh.controller;


import com.gjh.domain.system.Module;
import com.gjh.domain.system.User;
import com.gjh.service.system.ModuleService;
import com.gjh.service.system.UserService;
import com.qiniu.util.Md5;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpRequest;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.session.HttpServletSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController{
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpSession session;
    @Autowired
    private UserService userService;
    @Autowired
    private ModuleService moduleService;

	@RequestMapping("/login")
	public String login(String email,String password) {
	    //判断用户名密码是否为空
        if(StringUtils.isEmpty(email)||StringUtils.isEmpty(password)){
            request.setAttribute("error","用户名或密码不能为空");
            return "forward:/login.jsp";
        }
//        1.创建令牌
        password=new Md5Hash(password,email,2).toString();
        UsernamePasswordToken token = new UsernamePasswordToken(email, password);
//        2.获取主题
        Subject subject = SecurityUtils.getSubject();
//        3.开始认证
        try {
            subject.login(token);
        }catch (AuthenticationException e){
            request.setAttribute("error","邮箱或密码错误");
            return "forward:/login.jsp";
        }
        //页面需要在session中取数据
        User user = (User) subject.getPrincipal();

        session.setAttribute("loginUser",user);
        List<Module> moduleList=moduleService.findModuleListByUser(user);
        session.setAttribute("modules",moduleList);
        return "home/main";









       /* User user=userService.findByEmail(email);
        if(user==null){
            request.setAttribute("error","邮箱错误");
            return "forward:/login.jsp";
        }else {
            String password_db = user.getPassword();
            String password_page = new Md5Hash(password, email, 2).toString();
            if(!password_db.equals(password_page)){
                request.setAttribute("error","密码不正确");
                return "forward:/login.jsp";
            }
        }
        session.setAttribute("loginUser",user);
//        根据用户查询菜单
        List<Module> moduleList=moduleService.findModuleListByUser(user);
        session.setAttribute("modules",moduleList);
		return "home/main";*/
	}

    //退出
    @RequestMapping(value = "/logout",name="用户登出")
    public String logout(){
        SecurityUtils.getSubject().logout();   //登出
        return "forward:login.jsp";
    }

    @RequestMapping("/home")
    public String home(){
	    return "home/home";
    }
}
